#include "common_api.h"

void FM1906_Init(void)
{

    FM1906_Write_Command(SYSEN);        //打开系统振荡器
    FM1906_Write_Command(LCDON);        //开LCD偏压
    FM1906_Write_Command(BIAS1_3_4COM); //LCD 1/3偏压选项，4个公共口
    FM1906_Write_Command(RC_256K);      //系统时钟源，片内RC
}
/************************
 * FM1906写入命令
 * @param cmd 命令
 */
void FM1906_Write_Command(uint16_t cmd)
{
	uint8_t i;
    LCD_CS_LOW();
    for( i=0; i<12; i++)
    {
        LCD_WR_LOW();
        if (cmd & 0x800)    LCD_DATA_HIGH();
        else                LCD_DATA_LOW();
        Delay_us(1);
        LCD_WR_HIGH();
        Delay_us(1);
        cmd = (uint16_t)(cmd << 1);
    }
    LCD_CS_HIGH();
}
/************************
 * FM1906写数据
 * @param data
 * @param num
 */
void FM1906_Write_Data(uint8_t data,uint8_t num)
{
	uint8_t i;
    for( i=0; i<num; i++)
    {
        LCD_WR_LOW();
        if (data & 0x80)    LCD_DATA_HIGH();
        else                LCD_DATA_LOW();
        Delay_us(1);
        LCD_WR_HIGH();
        Delay_us(1);
        data = (uint8_t)(data << 1);
    }
}
/**************
 *  FM1906写一个地址和写数据
 * @param addr
 * @param data
 */
void FM1906_Write_OneData (uint8_t addr, uint8_t data)
{
    LCD_CS_LOW();
    Delay_us(1);
    FM1906_Write_Data(0xA0, 3); //写模式
    FM1906_Write_Data((uint8_t)(addr<<2),6);
    FM1906_Write_Data(data,4);
    LCD_CS_HIGH();
    Delay_us(1);
}
/***************
 * 发送命令
 * @param cmd
 */
void FM1906_Write_Cmd (uint8_t cmd)
{
    LCD_CS_LOW();
    Delay_us(1);
    FM1906_Write_Data(0x80, 3);      //写入标志代码100 命令模式
    FM1906_Write_Data(cmd, 9);       //写入9位数据位，其中前8位位Command命令，最后1位任意
    LCD_CS_HIGH();
    Delay_us(1);
}

/********************************
 * 发送N个数据到1906
 * @param addr
 * @param data
 */
void FM1906_Write_NumData (uint8_t addr, uint8_t * data,uint8_t num)
{
	 uint8_t i=0;
	
     LCD_CS_LOW();
     Delay_us(2);
     FM1906_Write_Data(0xA0, 3);      //写入标志码101
     FM1906_Write_Data((uint8_t)(addr<<2),6);         //写入6位数地址addr0
     for(i = 0; i < num; i++ )
     {
         FM1906_Write_Data(*data, 8);
         data++;
     }
     LCD_CS_HIGH();
     Delay_us(2);

}
void ClrLcd1 (void)
{
    uint8_t i=0; 
    
    LCD_CS_LOW();;
    Delay_us(2);
    FM1906_Write_Data(0xA0, 3);      //101
    FM1906_Write_Data(0, 6);      	//addr000 000
    for(i = 0; i < 30; i++ )
    {
          FM1906_Write_Data(0xFF, 8);      
    }  
    
    LCD_CS_HIGH();
    Delay_us(2);
}

void ClrLcd0 (void)
{
    uint8_t i=0; 
    
    LCD_CS_LOW();
    Delay_us(2);
    FM1906_Write_Data(0xA0, 3);      //101
    FM1906_Write_Data(0, 6);      	//addr000 000??
    for(i = 0; i < 30; i++ )
    {
          FM1906_Write_Data(0, 8);     
    }
    
    LCD_CS_HIGH();
    Delay_us(2);
}
